﻿using chatGUI.SingeltonData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;

namespace chatGUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public delegate void changeChatPanel(string uid, bool isGroup);
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            Messenger.Default.Register<string>(this, "clearChatPanel", clearChatPanel);
            loadAvatar("../../../resources/image/icon.jfif");
            initFriend();
        }
        private void loadAvatar(string url)
        {
            Uri uri = new Uri(url, UriKind.Relative);
            BitmapImage bitmapImage = new BitmapImage(uri);
            userAvatar.Source = bitmapImage;
        }
        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
        private void ButtonMinimize_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }
        private void initChat(string uid)
        {
            UserControls.chatAndDetailsControl chatAndDetailsControl = new UserControls.chatAndDetailsControl();
            switchChatAndNothingContent.Children.Clear();
            switchChatAndNothingContent.Children.Add(chatAndDetailsControl);
            Messenger.Default.Send(uid, "initChatMessageFormDataBase");
        }
        private void initFriend()
        {
            UserControls.friendListControl friendListControl = new UserControls.friendListControl();
            friendListControl.changeChatPanelEvent += FriendListControl_changeChatPanelEvent;
            switchFriendAndFriendListContent.Children.Clear();
            switchFriendAndFriendListContent.Children.Add(friendListControl);
        }
        private void FriendListControl_changeChatPanelEvent(string uid, bool isGroup)
        {
            userData.Instance.nowChattingWith = uid;
            userData.Instance.nowChattingWithGroup = isGroup;
            initChat(uid);
        }
        private void clearChatPanel(string clear)
        {
            switchChatAndNothingContent.Children.Clear();
        }
    }
}
